package com.yanfan.data.service.impl;

import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.domain.entity.SysDept;
import com.yanfan.common.core.domain.entity.SysUser;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.StringUtils;
import com.yanfan.data.domain.DataSimCard;
import com.yanfan.data.domain.DataSimCardRequestParam;
import com.yanfan.data.mapper.DataSimCardMapper;
import com.yanfan.data.mapper.DataSimCardRequestParamMapper;
import com.yanfan.data.service.DataSimCardService;
import com.yanfan.system.mapper.SysDeptMapper;
import com.yanfan.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.yanfan.common.utils.SecurityUtils.getLoginUser;

/**
 * 流量卡Service业务层处理
 *
 * @author kerwincui
 * @date 2024-07-11
 */
@Service
public class DataSimCardServiceImpl implements DataSimCardService {
    @Autowired
    private DataSimCardMapper dataSimCardMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private DataSimCardRequestParamMapper dataSimCardRequestParamMapper;


    /**
     * 查询流量卡
     *
     * @param id 流量卡主键
     * @return 流量卡
     */
    @Override
    @Transactional
    public DataSimCard selectDataSimCardById(Long id) {
        return dataSimCardMapper.selectDataSimCardById(id);
    }

    /**
     * 查询流量卡列表
     *
     * @param dataSimCard 流量卡
     * @return 流量卡
     */
    @Override
    public List<DataSimCard> selectDataSimCardList(DataSimCard dataSimCard) {
        return dataSimCardMapper.selectDataSimCardList(dataSimCard);
    }

    /**
     * 新增流量卡
     *
     * @param dataSimCard 流量卡
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertDataSimCard(DataSimCard dataSimCard, DataSimCardRequestParam dataSimCardRequestParam) {

        AjaxResult ajaxResult = new AjaxResult();
        //查询data_sim_card_request_param表中是否已有数据
        DataSimCardRequestParam dataSimCardRequestParam1 = dataSimCardRequestParamMapper.selectDataSimCardRequestParam(dataSimCardRequestParam.getAppId(), dataSimCardRequestParam.getAppSecret(), dataSimCardRequestParam.getIccId());

        if (null != dataSimCardRequestParam1) {
            ajaxResult.put("success", false);

        } else {
            //未找到数据 进行添加
            dataSimCardRequestParam.setCreateTime(DateUtils.getNowDate());
            dataSimCardRequestParamMapper.insertDataSimCardRequestParam(dataSimCardRequestParam);
            dataSimCard.setDscRequestParamId(dataSimCardRequestParam.getId());

            SysUser sysUser = getLoginUser().getUser();
            dataSimCard.setDeptId(sysUser.getDeptId());
            dataSimCard.setCreateTime(DateUtils.getNowDate());
            int i = dataSimCardMapper.insertDataSimCard(dataSimCard);
            if (i > 0) {
                ajaxResult.put("success", true);
            }
        }
        return ajaxResult;
    }


    /**
     * 流量卡分配列表
     *
     * @param dataSimCard 流量卡
     * @return 结果
     */
    @Override
    public List<DataSimCard> selectDataSimCardFpList(DataSimCard dataSimCard) {
        SysUser sysUser = getLoginUser().getUser();
        if(sysUser.getUserId()==1){
            dataSimCard.setDeptId(sysUser.getDeptId());
        }
        return dataSimCardMapper.selectDataSimCardFpList(dataSimCard);
    }

    /**
     * 分配流量卡
     *
     * @param deptId
     * @param dataSimCardIds
     * @return
     */
    @Override
    public AjaxResult distribution(Long deptId, String dataSimCardIds) {

        SysDept sysDept = sysDeptMapper.selectDeptById(deptId);
        if (null == sysDept || null == sysDept.getDeptUserId()) {
            return AjaxResult.error("机构不存在或未绑定管理员，请调整后重试！");
        }
        Long deptUserId = sysDept.getDeptUserId();
        SysUser sysUser = userService.selectUserById(deptUserId);
        if (null == sysUser) {
            return AjaxResult.error("机构管理员不存在");
        }

        List<String> dataSimCardList = StringUtils.str2List(dataSimCardIds, ",", true, true);
        int result = dataSimCardMapper.updateDataSimCardDeptId(deptId, dataSimCardList);
        return result > 0 ? AjaxResult.success("分配流量卡成功") : AjaxResult.error("分配流量卡失败");
    }


    /**
     * 根据dsc_request_param_id修改流量卡
     *
     * @param dataSimCard 流量卡
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDataSimCard(DataSimCard dataSimCard) {
        dataSimCard.setUpdateTime(DateUtils.getNowDate());
        return dataSimCardMapper.updateDataSimCard(dataSimCard);
    }


    /**
     * 批量删除流量卡
     *
     * @param ids 需要删除的流量卡主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDataSimCardByIds(Long[] ids) {
        // 查找对应的流量卡请求参数表数据id
        Long[] ids2 = new Long[ids.length];
        for (int i = 0; i < ids.length; i++) {
            DataSimCard dataSimCard = dataSimCardMapper.selectDataSimCardById(ids[i]);
            if (null != dataSimCard) {
                ids2[i] = dataSimCard.getDscRequestParamId();
            }

        }
        // 删除对应的流量卡请求参数表数据
        dataSimCardRequestParamMapper.deleteDataSimCardRequestParamByIds(ids2);

        return dataSimCardMapper.deleteDataSimCardByIds(ids);
    }

    /**
     * 删除流量卡信息
     *
     * @param id 流量卡主键
     * @return 结果
     */
    @Override
    public int deleteDataSimCardById(Long id) {
        return dataSimCardMapper.deleteDataSimCardById(id);
    }


}
